<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>集合的使用 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter04.html" />
    
    
    <link rel="prev" href="./chapter02/section02.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="3"
        data-chapter-title="集合的使用"
        data-filepath="chapter03.md"
        data-basepath="."
        data-revision="Sun Feb 09 2020 19:04:23 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        元组的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02/index.html">
            
                
                    <a href="./chapter02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        字典的基本使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter02/section01.html">
            
                
                    <a href="./chapter02/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        字典的增删改查
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter02/section02.html">
            
                
                    <a href="./chapter02/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        字典的遍历
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        集合的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        转换相关
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        通用方法
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="set&#x7684;&#x4F7F;&#x7528;">set&#x7684;&#x4F7F;&#x7528;</h1>
<p>&#x96C6;&#x5408;&#xFF08;set&#xFF09;&#x662F;&#x4E00;&#x4E2A;<span style="color:red;font-weight:700">&#x65E0;&#x5E8F;&#x7684;&#x4E0D;&#x91CD;&#x590D;&#x5143;&#x7D20;</span>&#x5E8F;&#x5217;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5927;&#x62EC;&#x53F7; <strong>{ }</strong> &#x6216;&#x8005; <strong>set()</strong> &#x51FD;&#x6570;&#x521B;&#x5EFA;&#x96C6;&#x5408;&#x3002;</p>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7A7A;&#x96C6;&#x5408;&#x5FC5;&#x987B;&#x7528; set() &#x800C;&#x4E0D;&#x662F; { }&#xFF0C;&#x56E0;&#x4E3A; { } &#x662F;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7A7A;&#x5B57;&#x5178;&#x3002;</strong></p>
<p>&#x521B;&#x5EFA;&#x683C;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-python">parame = {value01,value02,...}
&#x6216;&#x8005;
set(value)
</code></pre>
<h2 id="&#x6DFB;&#x52A0;&#x5143;&#x7D20;">&#x6DFB;&#x52A0;&#x5143;&#x7D20;</h2>
<p><strong>&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</strong></p>
<pre><code class="lang-python">s.add(x)
</code></pre>
<p>&#x5C06;&#x5143;&#x7D20; x &#x6DFB;&#x52A0;&#x5230;&#x96C6;&#x5408; s &#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x5143;&#x7D20;&#x5DF2;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x4E0D;&#x8FDB;&#x884C;&#x4EFB;&#x4F55;&#x64CD;&#x4F5C;&#x3002;</p>
<pre><code class="lang-python">&gt;&gt;&gt;thisset = set((<span class="hljs-string">&quot;Google&quot;</span>, <span class="hljs-string">&quot;Runoob&quot;</span>, <span class="hljs-string">&quot;Taobao&quot;</span>))
<span class="hljs-prompt">&gt;&gt;&gt; </span>thisset.add(<span class="hljs-string">&quot;Facebook&quot;</span>)
<span class="hljs-prompt">&gt;&gt;&gt; </span>print(thisset)
{<span class="hljs-string">&apos;Taobao&apos;</span>, <span class="hljs-string">&apos;Facebook&apos;</span>, <span class="hljs-string">&apos;Google&apos;</span>, <span class="hljs-string">&apos;Runoob&apos;</span>}
</code></pre>
<p>&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x5143;&#x7D20;&#xFF0C;&#x4E14;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x662F;&#x5217;&#x8868;&#xFF0C;&#x5143;&#x7EC4;&#xFF0C;&#x5B57;&#x5178;&#x7B49;&#xFF0C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">s.update( x )
</code></pre>
<p>x &#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;&#xFF0C;&#x7528;&#x9017;&#x53F7;&#x5206;&#x5F00;&#x3002;</p>
<pre><code class="lang-python">&gt;&gt;&gt;thisset = set((<span class="hljs-string">&quot;Google&quot;</span>, <span class="hljs-string">&quot;Runoob&quot;</span>, <span class="hljs-string">&quot;Taobao&quot;</span>))
<span class="hljs-prompt">&gt;&gt;&gt; </span>thisset.update({<span class="hljs-number">1</span>,<span class="hljs-number">3</span>})
<span class="hljs-prompt">&gt;&gt;&gt; </span>print(thisset)
{<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-string">&apos;Google&apos;</span>, <span class="hljs-string">&apos;Taobao&apos;</span>, <span class="hljs-string">&apos;Runoob&apos;</span>}
<span class="hljs-prompt">&gt;&gt;&gt; </span>thisset.update([<span class="hljs-number">1</span>,<span class="hljs-number">4</span>],[<span class="hljs-number">5</span>,<span class="hljs-number">6</span>])  
<span class="hljs-prompt">&gt;&gt;&gt; </span>print(thisset)
{<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-string">&apos;Google&apos;</span>, <span class="hljs-string">&apos;Taobao&apos;</span>, <span class="hljs-string">&apos;Runoob&apos;</span>}
</code></pre>
<h2 id="&#x79FB;&#x9664;&#x5143;&#x7D20;">&#x79FB;&#x9664;&#x5143;&#x7D20;</h2>
<p>&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">s.remove( x )
</code></pre>
<p>&#x5C06;&#x5143;&#x7D20; x &#x4ECE;&#x96C6;&#x5408; s &#x4E2D;&#x79FB;&#x9664;&#xFF0C;&#x5982;&#x679C;&#x5143;&#x7D20;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x4F1A;&#x53D1;&#x751F;&#x9519;&#x8BEF;&#x3002;</p>
<pre><code class="lang-python">&gt;&gt;&gt;thisset = set((<span class="hljs-string">&quot;Google&quot;</span>, <span class="hljs-string">&quot;Runoob&quot;</span>, <span class="hljs-string">&quot;Taobao&quot;</span>))
<span class="hljs-prompt">&gt;&gt;&gt; </span>thisset.remove(<span class="hljs-string">&quot;Taobao&quot;</span>)
<span class="hljs-prompt">&gt;&gt;&gt; </span>print(thisset)
{<span class="hljs-string">&apos;Google&apos;</span>, <span class="hljs-string">&apos;Runoob&apos;</span>}
<span class="hljs-prompt">&gt;&gt;&gt; </span>thisset.remove(<span class="hljs-string">&quot;Facebook&quot;</span>)   <span class="hljs-comment"># &#x4E0D;&#x5B58;&#x5728;&#x4F1A;&#x53D1;&#x751F;&#x9519;&#x8BEF;</span>
Traceback (most recent call last):
  File <span class="hljs-string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
KeyError: <span class="hljs-string">&apos;Facebook&apos;</span>
&gt;&gt;&gt;
</code></pre>
<p>&#x6B64;&#x5916;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x4E5F;&#x662F;&#x79FB;&#x9664;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x4E14;&#x5982;&#x679C;&#x5143;&#x7D20;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x4E0D;&#x4F1A;&#x53D1;&#x751F;&#x9519;&#x8BEF;&#x3002;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-python">s.discard( x )
</code></pre>
<pre><code class="lang-python">&gt;&gt;&gt;thisset = set((<span class="hljs-string">&quot;Google&quot;</span>, <span class="hljs-string">&quot;Runoob&quot;</span>, <span class="hljs-string">&quot;Taobao&quot;</span>))
<span class="hljs-prompt">&gt;&gt;&gt; </span>thisset.discard(<span class="hljs-string">&quot;Facebook&quot;</span>)  <span class="hljs-comment"># &#x4E0D;&#x5B58;&#x5728;&#x4E0D;&#x4F1A;&#x53D1;&#x751F;&#x9519;&#x8BEF;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>print(thisset)
{<span class="hljs-string">&apos;Taobao&apos;</span>, <span class="hljs-string">&apos;Google&apos;</span>, <span class="hljs-string">&apos;Runoob&apos;</span>}
</code></pre>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x968F;&#x673A;&#x5220;&#x9664;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">s.pop()
</code></pre>
<pre><code class="lang-python">thisset = set((<span class="hljs-string">&quot;Google&quot;</span>, <span class="hljs-string">&quot;Runoob&quot;</span>, <span class="hljs-string">&quot;Taobao&quot;</span>, <span class="hljs-string">&quot;Facebook&quot;</span>))
x = thisset.pop()

print(x)
print(thisset)
</code></pre>
<h2 id="set&#x5E38;&#x89C1;&#x65B9;&#x6CD5;&#x5217;&#x8868;">set&#x5E38;&#x89C1;&#x65B9;&#x6CD5;&#x5217;&#x8868;</h2>
<table class="reference">
<tbody><tr>
<th>&#x65B9;&#x6CD5;</th>
<th>&#x63CF;&#x8FF0;</th>
</tr>
<tr><td>add()</td><td>&#x4E3A;&#x96C6;&#x5408;&#x6DFB;&#x52A0;&#x5143;&#x7D20;</td></tr>
<tr><td>clear()</td><td>&#x79FB;&#x9664;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5143;&#x7D20;</td></tr>
<tr><td>copy()</td><td>&#x62F7;&#x8D1D;&#x4E00;&#x4E2A;&#x96C6;&#x5408;</td></tr>
<tr><td>pop()</td><td>&#x968F;&#x673A;&#x79FB;&#x9664;&#x5143;&#x7D20;</td></tr>
<tr><td>remove()</td><td>&#x79FB;&#x9664;&#x6307;&#x5B9A;&#x5143;&#x7D20;</td></tr>
<tr>
  <td>union</td><td>&#x8FD4;&#x56DE;&#x4E24;&#x4E2A;&#x96C6;&#x5408;&#x7684;&#x5E76;&#x96C6;</td>
</tr>
<tr><td>update()</td><td>&#x7ED9;&#x96C6;&#x5408;&#x6DFB;&#x52A0;&#x5143;&#x7D20;</td></tr>
<tr>
  <td>difference()</td><td>&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x96C6;&#x5408;&#x7684;&#x5DEE;&#x96C6;</td>
</tr>
<tr>
  <td>difference_update()</td><td>&#x79FB;&#x9664;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x8BE5;&#x5143;&#x7D20;&#x5728;&#x6307;&#x5B9A;&#x7684;&#x96C6;&#x5408;&#x4E5F;&#x5B58;&#x5728;&#x3002;</td>
</tr>
<tr><td>discard()</td><td>&#x5220;&#x9664;&#x96C6;&#x5408;&#x4E2D;&#x6307;&#x5B9A;&#x7684;&#x5143;&#x7D20;</td></tr>
<tr>
  <td>intersection()</td><td>&#x8FD4;&#x56DE;&#x96C6;&#x5408;&#x7684;&#x4EA4;&#x96C6;</td>
</tr>
<tr><td>intersection_update()</td><td>
  &#x5220;&#x9664;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x8BE5;&#x5143;&#x7D20;&#x5728;&#x6307;&#x5B9A;&#x7684;&#x96C6;&#x5408;&#x4E2D;&#x4E0D;&#x5B58;&#x5728;&#x3002;</td></tr>
  <tr>
    <td>isdisjoint()</td><td>&#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x96C6;&#x5408;&#x662F;&#x5426;&#x5305;&#x542B;&#x76F8;&#x540C;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x8FD4;&#x56DE; True&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE; False&#x3002;</td>
  </tr>
  <tr>
    <td>issubset()</td><td>&#x5224;&#x65AD;&#x6307;&#x5B9A;&#x96C6;&#x5408;&#x662F;&#x5426;&#x4E3A;&#x8BE5;&#x65B9;&#x6CD5;&#x53C2;&#x6570;&#x96C6;&#x5408;&#x7684;&#x5B50;&#x96C6;&#x3002;</td>
  </tr>
<tr><td>issuperset()</td><td>&#x5224;&#x65AD;&#x8BE5;&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x96C6;&#x5408;&#x662F;&#x5426;&#x4E3A;&#x6307;&#x5B9A;&#x96C6;&#x5408;&#x7684;&#x5B50;&#x96C6;</td></tr>
<tr>
    <td>symmetric_difference()</td><td>&#x8FD4;&#x56DE;&#x4E24;&#x4E2A;&#x96C6;&#x5408;&#x4E2D;&#x4E0D;&#x91CD;&#x590D;&#x7684;&#x5143;&#x7D20;&#x96C6;&#x5408;&#x3002;</td>
  </tr>
<tr><td>symmetric_difference_update()</td><td>
  &#x79FB;&#x9664;&#x5F53;&#x524D;&#x96C6;&#x5408;&#x4E2D;&#x5728;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x96C6;&#x5408;&#x76F8;&#x540C;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x5E76;&#x5C06;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x96C6;&#x5408;&#x4E2D;&#x4E0D;&#x540C;&#x7684;&#x5143;&#x7D20;&#x63D2;&#x5165;&#x5230;&#x5F53;&#x524D;&#x96C6;&#x5408;&#x4E2D;&#x3002; </td></tr>
</tbody></table>



<h2 id="&#x7EC3;&#x4E60;">&#x7EC3;&#x4E60;</h2>
<p>&#x6709;&#x4E00;&#x4E2A;&#x65E0;&#x5E8F;&#x4E14;&#x5143;&#x7D20;&#x6570;&#x636E;&#x91CD;&#x590D;&#x7684;&#x5217;&#x8868;nums, <code>nums=[5,8,7,6,4,1,3,5,1,8,4]</code>,&#x8981;&#x6C42;&#x5BF9;&#x8FD9;&#x4E2A;&#x5217;&#x8868;&#x91CC;&#x7684;&#x5143;&#x7D20;&#x53BB;&#x91CD;&#xFF0C;&#x5E76;&#x8FDB;&#x884C;&#x964D;&#x5E8F;&#x6392;&#x5E8F;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x65B9;&#x6CD5;&#x4E00;&#xFF1A;&#x8C03;&#x7528;&#x5217;&#x8868;&#x7684;sort&#x65B9;&#x6CD5;</span>
nums2 = list(set(nums))
nums2.sort(reverse=<span class="hljs-keyword">True</span>)
print(nums2)

<span class="hljs-comment"># &#x65B9;&#x6CD5;&#x4E8C;&#xFF1A;&#x4F7F;&#x7528;sorted&#x5185;&#x7F6E;&#x51FD;&#x6570;</span>
print(sorted(list(set(nums)),reverse=<span class="hljs-keyword">True</span>))
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter02/section02.html" class="navigation navigation-prev " aria-label="Previous page: 字典的遍历"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter04.html" class="navigation navigation-next " aria-label="Next page: 转换相关"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
